package com.cuitrip.business.home.favorite;

import android.text.TextUtils;

import com.cuitrip.apiservice.TripBusiness;
import com.cuitrip.app.base.CtApiCallback;
import com.cuitrip.app.base.CtException;
import com.cuitrip.app.base.ListFetchCallback;
import com.cuitrip.business.home.trip.model.RecommendOutData;
import com.cuitrip.service.R;
import com.cuitrip.util.PlatformUtil;
import com.lab.network.config.CtResponseCode;
import com.lab.network.model.CtApiResponse;
import com.lab.network.model.IProxyCallback;
import com.lab.network.model.ProxyResult;
import com.lab.network.proxy.ApiProxy;
import com.lab.utils.LogHelper;
import com.lab.utils.MessageUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by baziii on 15/8/10.
 */
public class FavoritePresent<T extends TravelTrip> {
    public static final String TAG = "FavoritePresent";
    IFavoriteFetcher mFavoriteFetcher = new FavoriteFetcher();
    IFavoriteListView mFavoriteView;

    public FavoritePresent(IFavoriteListView iFavoriteView) {
        this.mFavoriteView = iFavoriteView;
    }

    protected class FavoriteFetcher implements IFavoriteFetcher {
        int defaultSize = 10;

        @Override
        public void getFavoriteList(final ListFetchCallback<TravelTrip> itemListFetchCallback) {
            TripBusiness.getLikes(new ApiProxy(new IProxyCallback() {
                @Override
                public boolean onProxyResult(ProxyResult<?> result) {
                    CtApiResponse response= (CtApiResponse) result.getData();
                    if(response!=null && response.code==CtResponseCode.RESULT_OK){
                        ArrayList<TravelTrip> resultList = new ArrayList<>();
                        if(response.result instanceof RecommendOutData){
                            RecommendOutData recommendOutData= (RecommendOutData) response.result;
                            resultList.addAll(recommendOutData.getLists());
                        }
                        itemListFetchCallback.onSuc(resultList);
                    }else{
                        String msg;
                        if (response != null && !TextUtils.isEmpty(response.msg)) {
                            msg = response.msg;
                        } else {
                            msg = PlatformUtil.getInstance().getString(R.string.feedback_data_err);
                        }
                        itemListFetchCallback.onFailed(new CtException(msg));
                    }
                    return false;
                }
            }),0, defaultSize);
        }

        @Override
        public void getFavoriteListWithMore(int pattern, final ListFetchCallback<TravelTrip> itemListFetchCallback) {
            TripBusiness.getLikes(new ApiProxy(new IProxyCallback() {
                @Override
                public boolean onProxyResult(ProxyResult<?> result) {
                    CtApiResponse response = (CtApiResponse) result.getData();
                    LogHelper.e("omg", "result " + response.result);
                    if (response != null && response.code == CtResponseCode.RESULT_OK) {
                        ArrayList<TravelTrip> resultList = new ArrayList<>();
                        RecommendOutData recommendOutData = (RecommendOutData) response.result;
                        LogHelper.e("omg", "size " + recommendOutData.getLists().size());
                        resultList.addAll(recommendOutData.getLists());
                        itemListFetchCallback.onSuc(resultList);
                    } else {
                        String msg;
                        if (response != null && !TextUtils.isEmpty(response.msg)) {
                            msg = response.msg;
                        } else {
                            msg = PlatformUtil.getInstance().getString(R.string.feedback_data_err);
                        }
                        itemListFetchCallback.onFailed(new CtException(msg));
                    }
                    return false;
                }
            }), pattern, pattern + defaultSize);
        }

        @Override
        public void deleteFavorite(TravelTrip travelTrip, final CtApiCallback callback) {
            //  先删
            callback.onSuc();
            TripBusiness.unlikeService(new ApiProxy(new IProxyCallback() {
                @Override
                public boolean onProxyResult(ProxyResult<?> result) {
                    CtApiResponse response = (CtApiResponse) result.getData();
                    if (response != null && response.code == CtResponseCode.RESULT_OK) {
                        LogHelper.e("omg", " suc " + String.valueOf(response.result));
                    } else {
                        String msg;
                        LogHelper.e("omg", "failed ");
                        if (response != null && !TextUtils.isEmpty(response.msg)) {
                            msg = response.msg;
                        } else {
                            msg = PlatformUtil.getInstance().getString(R.string.feedback_data_err);
                        }
                        callback.onFailed(new CtException(msg));
                    }

                    return false;
                }
            }), travelTrip.getSid());
        }
    }


    public void requestLoadMore() {
        mFavoriteView.uiShowLoadMore();
        mFavoriteFetcher.getFavoriteListWithMore(mFavoriteView.getSize(), new ListFetchCallback<TravelTrip>() {
            @Override
            public void onSuc(List<TravelTrip> t) {
                LogHelper.e(TAG, "requestLoadMore onscu" + t.size());
                mFavoriteView.renderUIWithAppendData(t);
                mFavoriteView.uiHideLoadMore();
            }

            @Override
            public void onFailed(Throwable throwable) {

                MessageUtils.showToast(throwable.getMessage());
                LogHelper.e(TAG, "requestLoadMore onfailed");
                mFavoriteView.uiHideLoadMore();
            }
        });
    }

    public void onClick(T messageMode) {
        if (messageMode.isAvailable()) {
            mFavoriteView.jump(messageMode);
        } else {
            mFavoriteView.jumpUnvaliable(messageMode);
//            onMove(messageMode);
        }
    }

    public void onMove(final T messageMode) {
        mFavoriteFetcher.deleteFavorite(messageMode, new CtApiCallback() {
            @Override
            public void onSuc() {
                mFavoriteView.delete(messageMode);
            }

            @Override
            public void onFailed(CtException throwable) {
                MessageUtils.showToast(throwable.getMessage());
            }
        });
    }

    public void requestRefresh() {
        mFavoriteView.uiShowRefreshLoading();
        mFavoriteFetcher.getFavoriteList(new ListFetchCallback<TravelTrip>() {
            @Override
            public void onSuc(List<TravelTrip> t) {
                LogHelper.e(TAG, "requestRefresh onsuc" + t.size());
                mFavoriteView.renderUIWithData(t);
                mFavoriteView.uiHideRefreshLoading();
            }

            @Override
            public void onFailed(Throwable throwable) {
                LogHelper.e(TAG, "requestRefresh onfailed");
                MessageUtils.showToast(throwable.getMessage());
                mFavoriteView.uiHideRefreshLoading();
            }
        });
    }
}
